import util from '@/libs/util';
import utilDate from '@/util/getDate';
import CONSTANTS from '@/config/constants';
import Cookies from 'js-cookie';
    export default {
        name: 'exportable-table',
        data() {
            return {
                columns: [{
                    title:'#',
                    type:'index',
                },{
                    title: '报名预约号',
                    key: 'billcode'
                }, {
                    title: '学生姓名',
                    key: 'key-student',
                    render:function(h,params){
                        return h('span',params.row.student.name);
                    }
                }, {
                    title: '班级名称',
                    key: 'key-course',
                     render: function (h, params) {
                     return h('span',params.row.course.name);
                }
                }, {
                    title: '支付总金额',
                    key: 'money',
                }, {
                    title: '创建时间',
                    key: 'createtime',
                    render:function(h,params){
                            return h('div',utilDate.getDate(params.row.createtime,'time'));
                    }
                },{
                     title: '支付截至时间',
                    key: 'chargevalidity',
                    render:function(h,params){
                          return h('div',utilDate.getDate(params.row.chargevalidity,'time'));
                    }
                },{
                        title:"清单状态",
                        key:"status",
                           render: function (h, params) {
                           let text ="";
                           let status =params.row.status;
                           if(status==1){
                                    text="未支付"
                           }else if(status==2){
                                text="已支付"
                           }else if (status==3){
                             text="申请退款"
                           }else if(status==4){
                               text="已退款"
                           }else if(status==5){
                                 text="已作废"
                           }
                            return h('div', {}, text);
                }
            },{
                    title: '操作',
                    key: 'action',
                    width: 300,
                    align: 'center',
                    render: (h, params) => {
                        let status=params.row.status;
                        if(status==1){
                            return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                            this.entity={};
                                            this.obj={};
                                            this.costDataList=[];
                                            this.classDataList=[];

                                            this.initCostPage(params);
                                            this.costVisible=true;
                                            this.entity=params.row
                                       /* this.doApprove(2,params.row.id);*/
                                    }
                                }
                            }, '缴费')
                        ]);
                        } 
                    }
                }],
                dataList:[],
                addVisible:false,//新增弹窗的显示和隐藏
                areaList:[],//区域列表
                queryData:{},//查询表单数据
                pageSize:5,//分页数据
                pageIndex:1,//分页数据
                total: 0,
                departmentList:[],
                selection:[],//选中的行
                obj:[],
                entity:{},
                costVisible:false,
                payType:'',
                classTitle:[{
                        title:"班级名称",
                        key:"name",
                },{
                   title:"班级代码",
                   key:"classCode",
                },{
                   title:"学年",
                   key:"classYear",
                },{
                   title:"学期",
                   key:"classSeasonName",
                },{
                   title:"专业",
                   key:"marjorName",
                },{
                   title:"负责教师",
                   key:"mainTeacherName",
                },{
                   title:"课程安排",
                   key:"classSchedules",
                },{
                   title:"录取方式",
                   key:"classApply",
                },{
                   title:"总费用",
                   key:"fee",
                }],
                classDataList:[],
                costDataList:[],
                costTitle:[{
                        title:"项目名称",
                        key:"name",
                },{
                   title:"金额（元）",
                   key:"cost",
                },{
                   title:"打折金额",
                   key:"discount",
                },{
                   title:"打折理由",
                   key:"reason",
                },],
            };
        },
        mounted() {
                this.init();
        },
        methods: {
            init:function(){//页面初始化
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
                    {CODE:'AREA_TYPE'}
                )).then(res => {//获得区域数据
                    if(res.data.code==200){
                        var data =res.data.data;
                        this.areaList=data;
                    }
                });

                util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
                    {CODE:'CLASS_SEASON'}
                )).then(res => {//学期数据
                    if(res.data.code==200){
                        var data =res.data.data;
                        this.classSeasonList=data;
                    }
                });
                this.doQuery();
            }, 
            doQuery:function(){
                var queryData=this.queryData;
                queryData.page=this.pageIndex;
                queryData.size=this.pageSize;
            util.ajax.post('/busine/bill/list', util.toFormData(queryData)).then(res => {
                    if (res.data.data) {
                        this.dataList=res.data.data.list;
                        this.total = res.data.data.total;
                    }
                });
            },
            initCostPage:function(params){
                    let student = params.row.student;
                    this.money=params.row.money
                    if(student.sex=='WOMAN'){
                        student.sex="女"
                    }else if(student.sex=='MAN'){
                        student.sex="男"
                    }
                    this.obj=student;
                    let course=params.row.course
                    let  teachCost='';
                    let materialsCost='';
                    let clothingCost='';
                    if(course!=null && course!=''){
                        teachCost=course.teachCost;
                        materialsCost=course.materialsCost;
                        clothingCost=course.clothingCost
                    }
                    let costDataList=[];
                    if(teachCost!=''){
                        let obj = {
                            'name':'教学费',
                            'cost':teachCost,
                            'discount':'',
                            'reason':'',
                        }
                        costDataList.push(obj);
                    }

                    if(materialsCost!=''){
                        let obj = {
                            'name':'材料费',
                            'cost':materialsCost,
                            'discount':'',
                            'reason':'',
                        }
                        costDataList.push(obj);
                    }

                    if(clothingCost!=''){
                        let obj = {
                            'name':'服装费',
                            'cost':clothingCost,
                            'discount':'',
                            'reason':'',
                        }
                        costDataList.push(obj);
                    }
                    this.costDataList=costDataList;
                    let classSchedules=course.classSchedules
                      let classSchedules_content=''
                    if(classSchedules!='' && classSchedules.length>0){
                        for(let i in classSchedules){ 
                              let startTime = classSchedules[i].classStartTime;
                                 let endTime = classSchedules[i].classEndTime;
                                 if(startTime!=null && startTime!=''){
                                   startTime=startTime.substring(0,2)+":"+startTime.substring(2,4)
                                 }
                                if(endTime!=null && endTime!=''){
                                 endTime= endTime.substring(0,2)+":"+endTime.substring(2,4)
                                 }

                            let classType = classSchedules[i].classType;
                            switch (classType){
                                case'1':
                                    classType="周一";
                                 break;
                                  case'2':
                                    classType="周二";
                                 break;
                                  case'3':
                                    classType="周三";
                                 break;
                                  case'4':
                                    classType="周四";
                                 break;
                                  case'5':
                                    classType="周五";
                                 break;
                                  case'6':
                                    classType="周六";
                                 break;
                                  case'7':
                                    classType="周日";
                                 break;
                                  case'11':
                                     classType="每日";
                                 break;
                                  case'00':
                                     classType="双数日";
                                 break;
                                  case'01':
                                    classType="单数日";
                                 break;
                                  case'15':
                                    classType="周一到周五";
                                 break;
                                  case'16':
                                    classType="周一到周六";
                                break;
                                  case'135':
                                    classType="一三五";
                                  break;
                                  case'246':
                                  classType="二四六";
                                 break;
                            }
                            classSchedules_content+=" "+classType+" "+startTime+"-"+endTime;
                        }
                    }
                    let courseObj={
                        "name":course.name,
                        "classCode":'',
                        "classYear":course.classYear,
                        'classSeasonName':course.classSeasonName,
                        'marjorName':course.marjorName,
                        'mainTeacherName':course.mainTeacherName,
                        'classApply':course.classApply,
                        'fee':params.row.money,
                        'classSchedules':classSchedules_content
                    }
                    this.classDataList.push(courseObj);
            },
            doPay:function(){
                let  payType = this.payType;
                if(payType==''){
                     this.$Message.error('请选择支付方式!');
                    return;
                }
                this.$Modal.confirm({
                        title: '提示',
                        content: '确认缴费？',
                        onOk: () => {
                             util.ajax.post('/busine/bill/doPay', util.toFormData({billId:this.entity.id,
                                payType:this.payType,payMoney:this.entity.money})).then(res => {
                            if(res.data.code==200){
                                if(res.data.message=='SUCCESS'){
                                    this.$Message.success('缴费成功！');
                                    this.pageIndex = 1;
                                    this.init();
                                }else{
                                    this.$Message.warning(res.data.message || '未知错误！');
                                }
                                this.costVisible=false;
                            }
                    });   
                        },
                        onCancel: () => {
                             this.costVisible=false;
                        }
                });
            },
            payTypeChange:function(res){
                        this.payType=res;
            }
        }
    };